/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.common.util;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.nio.charset.Charset;

/**
 * URL解码，数据内容的类型是 application/x-www-form-urlencoded。
 *
 * <pre>
 * 1. 将%20转换为空格 ;
 * 2. 将"%xy"转换为文本形式,xy是两位16进制的数值;
 * 3. 跳过不符合规范的%形式，直接输出
 * </pre>
 */
public class URLDecoder implements Serializable {
  private static final long serialVersionUID = 1L;

  private static final byte ESCAPE_CHAR = '%';

  /**
   * 解码
   *
   * @param str 包含URL编码后的字符串
   * @param charset 编码
   * @return 解码后的字符串
   */
  public static String decode(String str, Charset charset) {
    return StrUtils.str(decode(StrUtils.bytes(str, charset)), charset);
  }

  /**
   * 解码
   *
   * @param bytes url编码的bytes
   * @return 解码后的bytes
   */
  public static byte[] decode(byte[] bytes) {
    if (bytes == null) {
      return null;
    }
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream(bytes.length);
    int b;
    for (int i = 0; i < bytes.length; i++) {
      b = bytes[i];
      if (b == '+') {
        buffer.write(CharUtils.SPACE);
      } else if (b == ESCAPE_CHAR) {
        if (i + 1 < bytes.length) {
          final int u = CharUtils.digit16(bytes[i + 1]);
          if (u >= 0 && i + 2 < bytes.length) {
            final int l = CharUtils.digit16(bytes[i + 2]);
            if (l >= 0) {
              buffer.write((char) ((u << 4) + l));
              i += 2;
              continue;
            }
          }
        }
        // 跳过不符合规范的%形式
        buffer.write(b);
      } else {
        buffer.write(b);
      }
    }
    return buffer.toByteArray();
  }
}
